package cn.com.dbappsecurity.frame.datamodal;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

/**
 * 普通json格式模型类
 */
public class JsonDataModal implements IDataModal {
	
	protected Map<String,Object> dataObj = new HashMap<String,Object>();
	
	private boolean success;
	
	public JsonDataModal(boolean success){
		this.success = success;
		this.dataObj.put("success",success);
	}
	
	/**
	 * 构造方法
	 * @param success	成功或失败
	 * @param msg		成功或失败信息
	 */
	public JsonDataModal(boolean success,String msg){
		this.success = success;
		this.dataObj.put("success",success);
		if(!StringUtils.isEmpty(msg)){
			this.dataObj.put("msg",msg);
		}
	}
	
	/**
	 * 添加一个对象到容器
	 * @param key
	 * @param obj
	 */
	public void putObj(String key,Object obj){
		this.dataObj.put(key,obj);
	}
	
	@Override
	public void shipToResponse(HttpServletRequest req,HttpServletResponse response){
		try {
			MAPPER.writeValue(response.getWriter(),dataObj);
		} catch (Exception e) {
			LOG.error("",e);
		}
	}

	@Override
	public boolean isSuccess() {
		return success;
	}

}

